console.log($event, '$event');
const helper = new Helper(this)
const prefix = helper.getVariable('environmentType');
const stream = StreamServices.getInstance(prefix)

const { dataTransfer } = $event;

// async function handleAreaData(projectAreaCode, projectCode) {
//     const { areaRows, equipmentRows, fusesRows } = await _videoAccess.queryQuotaByArea(projectAreaCode, projectCode);
//     _videoAccess.renderTree([...areaRows, ...equipmentRows, ...fusesRows], projectAreaCode);
// }

// const handleFuse = async (data, cb = () => { }) => {
//     const res = await _videoAccess.getFuseByEquipmentInfo(data);
//     // 获取播放地址
//     const url = stream.getStreamWebRTCUrl();
//     if (!res) return;
//     cb();
//     _videoAccess.addPlayer({
//         ...res,
//         playerType: 'webrtc',
//         equipmentName: '融合预览',
//         url
//     });
// }

window.handleFuse = async (data, cb = () => { }) => {
    const res = await _videoAccess.getFuseByEquipmentInfo(data);
    // 获取播放地址
    const url = stream.getStreamWebRTCUrl();
    if (!res) return;
    cb();
    _videoAccess.addPlayer({
        ...res,
        playerType: 'webrtc',
        equipmentName: '融合预览',
        url
    });
}


if (!dataTransfer?.getData('data:source')) {
    layer.msg('未找到摄像头配置!');
} else {
    const index = $($event.target).closest('.sullivan-grid-item').data('index')
    const widgets = helper.get('三维组件');
    const sullivan = widgets.widgetObj.sullivan;
    const source = JSON.parse(dataTransfer?.getData('data:source'));
    window.spliceSource = source;
    window.spliceSullivan = sullivan;
    window.spliceIndex = index;

    // console.log('monitorSelect: => ', source);
    if (source.children) {
        layer.msg('未找到摄像头配置!');
    } else {

        const oldVideo = sullivan.cameraMap.get(+index);
        window.spliceOldVideo = oldVideo;
        console.log('oldVideo: => ', oldVideo, index, window.videoSpliceIndex);
        if (oldVideo) {
            // const { projectAreaCode, projectCode } = oldVideo;
            // layer.confirm('<dl><dt><h3>是否需要视频拼接？</h3></dt><!--<dd>拼接成功将融合两个视频。</dd>--></dl>', { icon: 3, title: '视频拼接' }, () => {
            //     handleFuse([oldVideo.data, source], () => {
            //         // 关闭旧的摄像头
            //         sullivan.close(index);
            //         // 保存拼接
            //         _videoAccess.insertVideoFuse([oldVideo.data, source]).then(res => {
            //             handleAreaData(projectAreaCode, projectCode)
            //             layer.msg('视频融合成功!');
            //         });
            //     })
            // }, function(){
            //     layer?.msg('取消');
            // });

            const modals = this.pageObj.getApiWidgetsByClassName('video-splice-modal');
            modals.forEach(v => v.showWidget());
        } else {
            _videoAccess.addPlayer(source);
        }
    }
    // console.log(source, 'source')
}